package com.mzj.saas.rent;

import com.haixiaoke.saas.repository.util.GsonUtil;
import com.mzj.saas.commons.enums.PaymentTypeEnum;
import com.mzj.saas.manager.AsyncManager;
import com.mzj.saas.mservice.sys.entity.WechatAccount;
import com.mzj.saas.mservice.sys.repository.WechatAccountRepository;
import com.mzj.saas.sdk.pay.callback.WxPayCallback;
import com.mzj.saas.thread.task.WxCallBackOrderTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 小程序支付-公寓订单回调
 *
 * @author panda
 * @date 2023-04-18
 */
@Slf4j
@Controller
@RequestMapping("/rent/wxPay")
public class WxPayCallBackController {

    @Autowired
    private WechatAccountRepository wechatAccountRepository;

    @RequestMapping("/apartment/callBack")
    @ResponseBody
    public void callBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
        WxPayCallback callBack = new WxPayCallback(null, request, response);
        Map<String, String> result = callBack.verifyResult();
        try {
            WechatAccount account = wechatAccountRepository.findByOrgIdAndType(Long.valueOf(result.get("attach")), PaymentTypeEnum.WECHAT.getValue());
            // 签名验证
            if (!callBack.verifySignature(account.getMchApiKey())) {
                log.info("签名验证失败：查询的商户id：{}，回调的订单id：{}，订单的组织id：{}", GsonUtil.toJson(account), result.get("outTradeNo"), result.get("attach"));
                return;
            }
            //异步处理回调业务
            AsyncManager.me().execute(WxCallBackOrderTask.callBackOrderPay(result));
        } catch (Exception e) {
            log.error("小程序支付--公寓订单回调发生了错误：{}", e.getLocalizedMessage());
            callBack.resultFail();
        }
        callBack.resultSuccess();
    }

    @RequestMapping("/relet/apartment/callBack")
    @ResponseBody
    public void reletCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
        WxPayCallback callBack = new WxPayCallback(null, request, response);
        Map<String, String> result = callBack.verifyResult();
        try {
            WechatAccount account = wechatAccountRepository.findByOrgIdAndType(Long.valueOf(result.get("attach")), PaymentTypeEnum.WECHAT.getValue());
            // 签名验证
            if (!callBack.verifySignature(account.getMchApiKey())) {
                log.info("签名验证失败：查询的商户id：{}，回调的订单id：{}，订单的组织id：{}", GsonUtil.toJson(account), result.get("outTradeNo"), result.get("attach"));
                return;
            }
            //异步处理回调业务
            AsyncManager.me().execute(WxCallBackOrderTask.callBackOrderRelet(result));
        } catch (Exception e) {
            log.error("小程序支付--公寓订单回调发生了错误：{}", e.getLocalizedMessage());
            callBack.resultFail();
        }
        callBack.resultSuccess();
    }

    @RequestMapping("/apartment/bill/callBack")
    @ResponseBody
    public void apartmentBillCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
        log.info("公寓账单回调 ------- start");
        WxPayCallback callBack = new WxPayCallback(null, request, response);
        Map<String, String> result = callBack.verifyResult();
        try {
            WechatAccount account = wechatAccountRepository.findByOrgIdAndType(Long.valueOf(result.get("attach")), PaymentTypeEnum.WECHAT.getValue());
            // 签名验证
            if (!callBack.verifySignature(account.getMchApiKey())) {
                log.info("签名验证失败：查询的商户id：{}，回调的订单id：{}，订单的组织id：{}", GsonUtil.toJson(account), result.get("outTradeNo"), result.get("attach"));
                return;
            }
            //异步处理回调业务
            AsyncManager.me().execute(WxCallBackOrderTask.callBackBill(result));
        } catch (Exception e) {
            log.error("小程序支付--公寓账单支付回调发生了错误：{}", e.getLocalizedMessage());
            callBack.resultFail();
        }
        callBack.resultSuccess();
    }
}
